package com.ktz.shoping.db;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

/**
 * 数据源初始化类
 * @author 开拓者
 *
 */
public class DBDataSourece {
	
	private static DataSource dataSource;

	/**
	 * 单例模式，让构造函数私有
	 */
	private DBDataSourece() {
		
	}
	/**
	 * 单例模式，获取DataSource对象
	 * @return DataSource对象
	 */
	public static DataSource getDataSource(){
		if(dataSource==null){
			try {
				Properties properties = new Properties();
				//将连接文件加载到properties对象里面来
				properties.load(DBDataSourece.class.getClassLoader().getResourceAsStream("db.properties"));
				//使用dbcp初始化datasource
				dataSource = BasicDataSourceFactory.createDataSource(properties);
			} catch (Exception e) {
				//抛出数据源初始化异常
				e.printStackTrace();
			}
		}
		return dataSource;
	}
}
